import http from "@/utils/http.js";

module.exports = {
	rightApi(apiName, params) {
		return new Promise((resolve, reject) => {
			http.Post(apiName, params).then(res => {
				resolve(res)
			})
		})
	},


	showLoading(title = "加载中...") {
		uni.showLoading({
			title: title,
			mask: true,
		})
	},

	//获取时间
	getDay: function(day, is_full, isStartDay, isNow) {
		var that = this;
		var today = new Date();
		var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
		today.setTime(targetday_milliseconds);
		var tYear = today.getFullYear();
		var tMonth = today.getMonth();
		var tDate = today.getDate();

		tMonth = that.handleMonth(tMonth + 1);
		tDate = that.handleMonth(tDate);

		let tH = today.getHours() < 10 ? '0' + today.getHours() : today.getHours()
		let tM = today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes()
		let tS = today.getSeconds() < 10 ? '0' + today.getSeconds() : today.getSeconds()
		if (is_full) {
			if (isStartDay) {
				return tYear + "-" + tMonth + "-01" + " 00:00:00";
			} else if (isNow) {
				return tYear + "-" + tMonth + "-" + tDate + " " + tH + ":" + tM + ":" + tS;
			} else {
				return tYear + "-" + tMonth + "-" + tDate + " 00:00:00";
			}
		} else {
			if (isStartDay) {
				return tYear + "-" + tMonth + "-01"
			} else {
				return tYear + "-" + tMonth + "-" + tDate;
			}
		}
	},

	getMonth: function(day) {
		var that = this;
		var today = new Date();
		var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
		today.setTime(targetday_milliseconds);
		var tYear = today.getFullYear();
		var tMonth = today.getMonth();
		var tDate = today.getDate();

		tMonth = that.handleMonth(tMonth + 1);
		tDate = that.handleMonth(tDate);


		return tYear + "-" + tMonth
	},

	//获取时间
	getDayCenter: function(day) {
		var that = this;
		var today = new Date();
		var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
		today.setTime(targetday_milliseconds);
		var tYear = today.getFullYear();
		var tMonth = today.getMonth();
		var tDate = today.getDate();

		tMonth = that.handleMonth(tMonth + 1);
		tDate = that.handleMonth(tDate);

		let tH = today.getHours() < 10 ? '0' + today.getHours() : today.getHours()
		let tM = today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes()
		let tS = today.getSeconds() < 10 ? '0' + today.getSeconds() : today.getSeconds()

		return tYear + "-" + tMonth + "-" + tDate + " 12:00:00";
	},
	//获取时间
	getDayMonth: function(day) {
		var that = this;
		var today = new Date();
		var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
		today.setTime(targetday_milliseconds);
		var tYear = today.getFullYear();
		var tMonth = today.getMonth();
		var tDate = today.getDate();

		tMonth = that.handleMonth(tMonth + 1);
		tDate = that.handleMonth(tDate);

		let tH = today.getHours() < 10 ? '0' + today.getHours() : today.getHours()
		let tM = today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes()
		let tS = today.getSeconds() < 10 ? '0' + today.getSeconds() : today.getSeconds()

		return tYear + "-" + tMonth;
	},

	js_date_time: function(unixtime, is_full) {
		var date = new Date(unixtime);
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		m = m < 10 ? ('0' + m) : m;
		var d = date.getDate();
		d = d < 10 ? ('0' + d) : d;
		var h = date.getHours();
		h = h < 10 ? ('0' + h) : h;
		var minute = date.getMinutes();
		var second = date.getSeconds();
		minute = minute < 10 ? ('0' + minute) : minute;
		second = second < 10 ? ('0' + second) : second;
		// return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;//年月日时分秒
		if (is_full) {
			return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
		} else {
			return y + '-' + m + '-' + d;
		}
	},

	handleMonth: function(month) {
		var m = month;
		if (month.toString().length == 1) {
			m = "0" + month;
		}
		return m;
	},
	setNavigationBarTitle(title) {
		uni.setNavigationBarTitle({
			title: title
		})
	},

	showToast(title) {
		uni.showToast({
			icon: "none",
			title: title
		})
	},

	showToastBack(title, delta) {
		uni.navigateBack({
			delta: delta
		})

		setTimeout(function() {
			uni.showToast({
				icon: "none",
				title: title
			})
		}, 1000)
	},

	getArtpptNum() {
		let date = new Date();
		let year = '' + date.getFullYear(); //获取当前年份  
		let mon = date.getMonth() + 1; //获取当前月份  
		let day = date.getDate() < 10 ? '0' + date.getDate() : +'' + date.getDate(); //获取当前日  
		let h = '' + date.getHours(); //获取小时  
		let m = '' + date.getMinutes(); //获取分钟  
		let s = '' + date.getSeconds(); //获取秒
		return year + '' + mon + '' + day + '' + h + '' + m + '' + s;
	}

}
